/**
 *
 * Copyright by Samuel Castro, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 */
package com.SySammy.Syraffle.Cmds;

import com.SySammy.Syraffle.Raffle;
import java.util.ArrayList;
import java.util.HashMap;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class RaffleCmds implements CommandExecutor {

    private Raffle plugin;
    public HashMap<String, ArrayList<Boolean>> Cfg = new HashMap<String, ArrayList<Boolean>>();
    public static boolean RaffleStarted = false;

    public RaffleCmds(Raffle aThis) {
        this.plugin = aThis;
    }

    @Override
    public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings) {
        String[] args = strings;
        String cmd = cmnd.getName().toLowerCase();
        if (cmd.equals("raffle")) {
            return RaffleMethod(cs, args);
        } else if (cmd.equals("buyraffle")) {
            return BuyRaffleMethod(cs, args);
        }
        return true;
    }

    private boolean RaffleMethod(CommandSender cs, String[] args) {
        try {
            Player p = (Player) cs;
            RaffleHandler Handler = new RaffleHandler(plugin);
            if (!plugin.casUseMakeRaffle(p)) {
                cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                return true;
            }
            if (RaffleStarted == true) {
                p.sendMessage(ChatColor.RED + "A Raffle is already running, try again later.");
                return true;
            }

            if (args.length <= 0) {
                return false;

            } else if (args[0].equalsIgnoreCase("-p") && args.length >= 3) {
                boolean SetRafflePrize = Handler.SetRafflePrize(p, args[1].toUpperCase(), Integer.parseInt(args[2]));
                SetConfig(p, 0, SetRafflePrize);
                return true;

            } else if (args[0].equalsIgnoreCase("-c") && args.length >= 3) {
                boolean SetRaffleCost = Handler.SetRaffleCost(p, args[1].toUpperCase(), Integer.parseInt(args[2]));
                SetConfig(p, 1, SetRaffleCost);
                return true;

            } else if (args[0].equalsIgnoreCase("-t") && args.length >= 3) {
                boolean SetRaffleTimeQnt = Handler.SetRaffleTimeQnt(p, Integer.parseInt(args[1]), Integer.parseInt(args[2]));
                SetConfig(p, 2, SetRaffleTimeQnt);
                return true;

            } else if (args[0].equalsIgnoreCase("-s") && args.length >= 1) {
                if (GetConfig(p, 0) == false) {
                    p.sendMessage(ChatColor.GREEN + "Please, use '-p' to specify Prize.");
                    return true;
                }
                if (GetConfig(p, 1) == false) {
                    p.sendMessage(ChatColor.GREEN + "Please, use '-c' to specify ticket cost.");
                    return true;
                }
                if (GetConfig(p, 2) == false) {
                    p.sendMessage(ChatColor.GREEN + "Please, use '-t' to specify quantiy and time.");
                    return true;
                }
                RaffleStarted = true;
                Handler.StartRaffle(p);
                return true;

            } else if (args[0].equalsIgnoreCase("-h") && args.length >= 1) {
                return false;
            }
            return false;
        } catch (Exception e) {
            cs.sendMessage(ChatColor.RED + "Input error, please try again.");
            return false;
        }
    }

    private void SetConfig(Player p, int i, boolean DO) {
        ArrayList<Boolean> MakeTrue;
        if (Cfg.get(p.getName()) == null) {
            MakeTrue = new ArrayList<Boolean>();
            MakeTrue.add(false);
            MakeTrue.add(false);
            MakeTrue.add(false);
            Cfg.put(p.getName(), MakeTrue);
        }
        MakeTrue = Cfg.get(p.getName());
        MakeTrue.set(i, DO);
        Cfg.put(p.getName(), MakeTrue);
    }

    private boolean GetConfig(Player p, int i) {
        if (Cfg.get(p.getName()) == null) {
            return false;
        }
        if (Cfg.get(p.getName()).get(i) == null) {
            return false;
        }
        return Cfg.get(p.getName()).get(i);
    }

    private boolean BuyRaffleMethod(CommandSender cs, String[] args) {
        try {
            Player p = (Player) cs;
            if (!plugin.casUseBuyRaffle(p)) {
                cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                return true;
            }
            if (RaffleStarted == false) {
                p.sendMessage(ChatColor.RED + "No raffle running, please try again later.");
                return true;
            }
            RaffleHandler Handler = new RaffleHandler(plugin);
            if (args[0].equalsIgnoreCase("-i") && args.length >= 1) {
                Handler.RaffleInfo(p);
                return true;
            } else if (args[0].equalsIgnoreCase("-h") && args.length >= 1) {
                return true;
            } else if (args.length <= 1) {
                Handler.BuyRaffle(p, Integer.parseInt(args[0]));
                return true;
            }
            return false;
        } catch (Exception e) {
            cs.sendMessage(ChatColor.RED + "Input error, please try again.");
            return false;
        }
    }
}
